﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;
using System.Globalization;

namespace blackhouse.BlackApart.Business.Opinions {
	public class DaOpinionCategory : IOpinionCategory {

		#region Fields

		private IOpinionsDataAccess dataAccess;
		private ImpOpinionCategory instance;

		#endregion

		#region Properties

		public int CategoryId {
			get { return this.instance.CategoryId; }
			private set { this.instance.CategoryId = value; }
		}

		public string Name {
			get { return this.instance.Name; }
			set { this.instance.Name = value; }
		}

		public string NameContinued {
			get { return this.instance.NameContinued; }
			set { this.instance.NameContinued = value; }
		}

		#endregion

		#region Constructors

		public DaOpinionCategory(IOpinionsDataAccess iOptionsDataAccess) {
			this.dataAccess = iOptionsDataAccess;
			this.instance = new ImpOpinionCategory();
		}

		private DaOpinionCategory(IOpinionsDataAccess iOptionsDataAccess, OpinionCategoryData data) {
			this.dataAccess = iOptionsDataAccess;
			this.instance = this.Convert(data);
		}

		#endregion

		#region Methods

		public IOpinionCategory[] GetCategories() {
			if (this.instance.Categories == null) {
				OpinionCategoryData[] data = this.dataAccess.GetOpinionsCategories(CultureInfo.CurrentCulture.Name);
				this.instance.Categories = Array.ConvertAll(data, c => new DaOpinionCategory(this.dataAccess, c));
			}
			return this.instance.GetCategories();
		}

		private ImpOpinionCategory Convert(OpinionCategoryData dataItem) {
			ImpOpinionCategory category = new ImpOpinionCategory();
			category.CategoryId = dataItem.CategoryId;
			category.Name = dataItem.Name;
			category.NameContinued = dataItem.NameContinued;
			return category;
		}

		#endregion

	}
}
